
local base_data = require '地图.游戏流程.天赋抽取.注册数据'

local tf_list = base_data.tf_list
local zh_list = base_data.jb_list
local group = base_data.group
local func1 = base_data.func1
local func2 = base_data.func2

ac.game:event '天赋-获得属性'(function(_,hero,temp)
    local player = hero.owner
    local show = player:get_data('天赋显示表') or {}
    local has = player:get_data('天赋拥有表') or {}
    local f = func1[temp.name]
    
    has[temp.name] = (has[temp.name] or 0) + 1

    if has[temp.name]<= temp['数量'] then
        load_item_state(hero,temp,1)
        if f then
            f(has[temp.name],hero,player,temp)
        end
        if has[temp.name]==1 then
            table.insert(show,temp)
            table.sort(show,function(a,b)
                return a.index<b.index
            end)
        end
        hero:notify('天赋-检查组合完成',hero,temp)

        player:set_data('刷新次数',0)

        if player:get_data('引导-杀敌兑换')==nil then
            player:set_data('引导-杀敌兑换',true)
            player:notify('引导-显示内容',player,'杀敌兑换','击杀怪物获得杀敌数，消耗杀敌可以在此处兑换奖励')
        end
    end
end)

ac.sync:event '玩家-选择抽卡'(function(_,player,index)
    local hero = player.hero
    local list = player:get_data('天赋选择表')
    local has = player:get_data('天赋拥有表') or {}
    local temp = list and list[index]
    if temp then
        player:set_data('天赋选择表',nil)

        hero:notify('天赋-获得属性',hero,temp)

        --没抽的返还
        local info = player:get_data('天赋抽取表')
        if info then
            for a,data in ipairs(list) do
                if a~=index and has[data.name]~=data['数量'] then
                    table.insert(info,{data,data['权重']})
                end
            end
        end
    end

end)

ac.game:event '玩家-抽取天赋'(function(_,player)
    local info = player:get_data('天赋抽取表')
    local list = player:get_data('天赋选择表')
    local import = player:get_data('重点权重') or {}
    local shows = {}
    local count = 3

    for a,data in ipairs(list or {}) do
        local jb = data['组合名称']
        if import[jb] then
            table.insert(info,{data,data['权重']*2})
        else
            table.insert(info,{data,data['权重']})
        end
    end
    for a=1,count do
        if info and #info>0 then
            local data,index = table.get_by_weight(info)
            table.remove(info,index)
            table.insert(shows,data)
        end
    end
    player:set_data('天赋选择表',shows)
    player:notify('玩家-显示抽卡',player,shows,true)
end)


ac.game:event '天赋-检查组合完成'(function(_,hero,temp)
    local player = hero.owner
    local has = player:get_data('天赋拥有表') or {}
    local import = player:get_data('重点权重') or {}
    local jb = temp['组合名称']
    local jb_list = temp.jb_list
    local open = ('完成组合-%s'):format(jb)
    if jb and jb_list and player:get_data(open)==nil then
        import[jb] = true
        for _,name in ipairs(jb_list) do
            local data = tf_list[name]
            if not has[name] or has[name]<data['数量'] then
                return
            end
        end
        local data = zh_list[jb]
        local f = func2[jb]
        player:set_data(open,true)
        load_item_state(hero,data,1)
        player:send_msg(('完成组合:|cffff00ff%s|r'):format(jb))
        if f then
            f(temp,hero,player)
        end
        player:notify('天赋-添加新组合',player,jb,true)
    end
end)


ac.game:event '天赋-添加新组合'(function(_,player,name,flag)
    local info = player:get_data('天赋抽取表') or {}
    for _,data in pairs(tf_list) do
        if data['卡池条件']==name then
            table.insert(info,{data,data['权重']})
            if flag then
                player:send_msg(('添加新卡牌:%s'):format(data.title or data.name))
            end
        end
    end
    player:set_data('天赋抽取表',info)
end)



register_hero(function(player,hero)
    player:set_data('天赋拥有表',{})
    player:set_data('天赋显示表',{})
    player:set_data('重点权重',{})
    player:notify('天赋-添加新组合',player,'初始池')
end)